<?php

namespace App\Containers\AppSection\Data\Tasks;

use App\Containers\AppSection\Data\Data\Repositories\CompanyRepository;
use App\Containers\AppSection\Data\Models\Company;
use App\Ship\Exceptions\CreateResourceFailedException;
use App\Ship\Parents\Tasks\Task;

class CreateDefaultCompanyTask extends Task
{
    protected CompanyRepository $repository;

    public function __construct(CompanyRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run(int $userId, $name = ''): Company
    {
        try {
            return $this->repository->create([
                'user_id' => $userId,
                'name' => $name,
            ]);
        } catch (Exception $exception) {
            throw new CreateResourceFailedException('Failed to create Company');
        }
    }
}
